<?php

namespace app\home\controller;

use app\common\controller\Home;

class Recharge extends Home
{
    // 客户模型
    protected $BusinessModel = null;

    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('Business.Business');
    }

    // 充值操作
    public function index()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param('money');

            $data = [
                'money' => $param
            ];

            // 根据登录id查询数据表
            $user = db('Business')->where('id',$this->LoginAuth['id'])->find();

            // 找出表中money字段并转换为浮点型
            $oldMoney = (float)$user['money'];
            // halt($oldMoney);

            // 将请求过来的值转换为浮点型,与表中数值相加，得到新的money值
            $newMoney = (float)$data['money'] + $oldMoney;
            // halt($newMoney);

            // 更新
            $result = db('Business')->where('id',$this->LoginAuth['id'])->setField('money',$newMoney);

            if ($result === FALSE) {
                $this->error('充值失败');
            } else {
                $this->success('充值成功',url('home/Recharge/index'),$newMoney);
            }
        }

        return $this->fetch();
    }
}
